(function(){

YAHOO.pendular.RepositoryFolderView = function(config){
    if (!config) {
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        repository: YAHOO.pendular.app.repository,
        path: "/"
    });
    this.config = config;
    this.repository = config.repository;
    this.path = config.path;
    this.onPathChange = new YAHOO.util.CustomEvent("pathChange");
};

YAHOO.lang.extend(YAHOO.pendular.RepositoryFolderView, Object, {
    init: function(){
        var el = document.createElement("div");
        el.setAttribute("id", this.path);
        el.setAttribute("class", "pendular-folderview");
        YAHOO.util.Dom.get("pendular-windows").appendChild(el);
        this.render();
        return el;
    },
    getEl: function(){
        return YAHOO.util.Dom.get(this.path);
    },
    renderBreadCrumbs: function(item){
        var html = "", separator = "/", count=0,
            repository = this.repository,
            dataTable = YAHOO.pendular.app.repositoryDataTable;
        while (count<3 && item.parent) {
            item = item.parent;
            html =  dataTable.renderLink(item, count) +
                    (count ? separator : "") + 
                    html;
            count++;
        }
        html =  "<div class=\"pendular-folderview-breadcrumbs\">" +
                html +
                "</div>";
        return html;
    },
    renderHeader: function(item){
        var html = this.renderBreadCrumbs(item);
        html += "<h1>" +
                item.localizedName +
                "</h1>" +
                item.description
        ;
        return html;
    },
    renderContentItem: function(item, i){
        var resultType, link, url, description, clickHandler;
        //favicon
        favIcon = "<span class=\"pendular-favicon\">&#160;&#160;&#160;&#160;</span>";
            
        //Link
        if (item.isDirectory) {
            resultType = "directory";
            url = "#";
        }
        else {
            resultType = item.extension;
            url = item.url;
        }
        clickHandler =  "YAHOO.pendular.app.repositoryDataTable.onResultLinkClicked.fire(this);" +
                        "return false;";
                        
        link = "<a class=\"pendular-result-link\"" +
                " tabindex=\"" + i + "\"" + 
                " title=\"" + item.key + "\"" + 
                " onclick=\"" + clickHandler + "\"" + 
                " href=\"" + YAHOO.lang.escapeHTML(url) + "\">" +
                item.localizedName +
                "</a>";

        //Description
        desc = YAHOO.lang.escapeHTML(item.description);
        desc = "<p class=\"pendular-result-desc\"" +
                " title=\"" + desc + "\"" +
                ">" + desc + "</p>";            
        
        return "<div" +
                " class=\"pendular-result pendular-result-type-" + resultType + "\">" +
                favIcon + 
                link + 
                desc +
                "</div>"
                ;
    },
    renderContents: function(item) {
        var html = "<div class=\"pendular-folderview-contents\">",
            children = item.children,
            key, i=0;
        for (key in children){
            html += this.renderContentItem(children[key], ++i);
        }
        html += "<div>";
        return html;
    },
    setPath: function(path) {
        var me = this;
        if (me.path===path) {
            return;
        }
        me.onPathChange.fire(me, me.path, path);
        me.getEl().id = path;
        me.path = path;
        me.render();
    },
    render: function(){
        var me = this,
            html = "",
            el = me.getEl(),
            item = me.repository.entries[me.path]
        ;
        html += me.renderHeader(item);
        html += me.renderContents(item);
        
        el.innerHTML = html;
    }
});

})();
